﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FilStartUps.DAL;
using FilStartUps.Helper;
using System.IO;


namespace FilStartUps.Account
{
    public partial class Registration : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (User.Identity.IsAuthenticated)
                {
                    LinkButton lkHome = (LinkButton)FilStartUps_PageNav1.FindControl("lbLgout");
                    lkHome.Visible = true;
                    LinkButton lklogin = (LinkButton)FilStartUps_PageNav1.FindControl("lblogin");
                    LinkButton lkregister = (LinkButton)FilStartUps_PageNav1.FindControl("lbRegister");
                    lklogin.Visible = false;
                    lkregister.Visible = false;
                }
              
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (CheckUser(txtemail.Text) == true)
            {
                if (Page.IsValid)
                {
                    using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
                    {
                        Member mem = new Member();
                        Profile profile = new Profile();

                        mem.emailaddress = txtemail.Text.Trim();
                        mem.confirmemailaddress = txtconfirmemail.Text.Trim();
                        mem.password = Helper.Helper.HashPassword(txtpassword.Text.Trim());
                        mem.validationcode = Helper.Helper.CreateActKey();
                        //TO UPPER -->To be followed
                        mem.confirmpassword = Helper.Helper.HashPassword(txtconfirmpassword.Text.Trim().ToUpper());
                        mem.datejoined = DateTime.Now;
                        mem.member_flag = false;
                        mem.roleidFK = 2;
                        mem.secretanswer = txtsecanswer.Text.Trim();
                        mem.secretquestion = ddlsecurityquestion.SelectedValue;
                        //Validate Fields();
                        db_connect.Members.Add(mem);
                        db_connect.SaveChanges();

                        System.Drawing.Image fils = System.Drawing.Image.FromFile("D:/Filstartups/Project/FilStartUps/Content/images/banner-5.png");
                        byte[] filsbase64 = ImageToBase64(fils, System.Drawing.Imaging.ImageFormat.Png);
                        profile.image = filsbase64;
                        profile.firstname = txtfirstname.Text.Trim();
                        profile.lastname = txtlastname.Text.Trim();
                        profile.memberidFK = mem.memberidPK;
                        db_connect.Profiles.Add(profile);
                        db_connect.SaveChanges();
                        Emailer.SendFilStartUpsRegistrationEmail(mem.emailaddress, mem.password, profile.firstname, profile.lastname, mem.validationcode);
                        //Email User
                        Response.Redirect("~/Account/AccountConfirmation.aspx");
                        
                    }
                }
                else
                {
                    //Invalid Check Captcha
                    lberror.Text = "Check Captcha";
                    lberror.Visible = true;
                }
            }
            else
            {
                //Label
                lberror.Text = "Email Address is already been used!";
                lberror.Visible = true;
            }
        }

        //Convert To Base64string
        public byte[] ImageToBase64(System.Drawing.Image image, System.Drawing.Imaging.ImageFormat format)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                //convert image to byte[]
                image.Save(ms, format);
                byte[] imageBytes = ms.ToArray();

                //Convert byte[] to base64string
                //string base64string = Convert.ToBase64String(imageBytes);
                return imageBytes;
            }
        }
        //Convert To Image
        public System.Drawing.Image Base64ToImage(string base64String)
        {
            // Convert Base64 String to byte[]
            byte[] imageBytes = Convert.FromBase64String(base64String);
            MemoryStream ms = new MemoryStream(imageBytes, 0,
              imageBytes.Length);

            // Convert byte[] to Image
            ms.Write(imageBytes, 0, imageBytes.Length);
            System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
            return image;
        }


        private bool CheckUser(string email)
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                var user_check = (from m in db_connect.Members
                                  where m.emailaddress.Equals(email)
                                  select m).SingleOrDefault();

               
                if (user_check == null) //if email is not in use return true
                {
                    return true;
                }
                else //email address is already been used
                {
                    return false;
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Home.aspx");
        }


    }
}